#include "input_func.h"

#define ADC_THRESHOLD           200UL

extern EventBits_t uxBits;
extern EventGroupHandle_t xEventGroup;

void ADC_Update(void)
{
    pins_channel_type_t swipe_turn;
    uint16_t turn_left_adc = 0, turn_right_adc = 0;
    uint16_t po_left_adc = 0, po_right_adc = 0;
    uint8_t turn_left_cnt = 0, turn_right_cnt = 0;
    uint8_t po_left_cnt = 0, po_right_cnt = 0;
    uint8_t swipe_turn_signal = 0;

    swipe_turn = (PINS_DRV_ReadPins(PTE)&(1<<9));
    turn_left_adc = ADC_GET_EXT0();
    turn_right_adc = ADC_GET_EXT1();
    po_left_adc = ADC_GET_EXT2();
    po_right_adc = ADC_GET_EXT3();
    swipe_turn_signal = (uint8_t)(swipe_turn >> 9);

    if(swipe_turn_signal)
    {
        xEventGroupSetBits(xEventGroup, EVENRGROUP_SWIPE);
    }
    else
    {
        xEventGroupClearBits(xEventGroup, EVENRGROUP_SWIPE);
    }

    if(turn_left_adc > ADC_THRESHOLD)
    {
        if(turn_left_cnt < 3)
        {
            turn_left_cnt++;
        }
    }
    else
    {
        turn_left_cnt = 0;
    }

    if(turn_left_cnt > 3)
    {
        xEventGroupSetBits(xEventGroup, EVENRGROUP_TL);
    }
    else
    {
        xEventGroupClearBits(xEventGroup, EVENRGROUP_TL);
    }

    if(turn_right_adc > ADC_THRESHOLD)
    {
        if(turn_right_cnt < 3)
        {
            turn_right_cnt++;
        }
    }
    else
    {
        turn_right_cnt = 0;
    }

    if(turn_right_cnt > 3)
    {
        xEventGroupSetBits(xEventGroup, EVENRGROUP_TR);
    }
    else
    {
        xEventGroupClearBits(xEventGroup, EVENRGROUP_TR);
    }

    if(po_left_adc > ADC_THRESHOLD)
    {
        if(po_left_cnt < 3)
        {
            po_left_cnt++;
        }
    }
    else
    {
        po_left_cnt = 0;
    }

    if(po_left_cnt > 3)
    {
        xEventGroupSetBits(xEventGroup, EVENRGROUP_PL);
    }
    else
    {
        xEventGroupClearBits(xEventGroup, EVENRGROUP_PL);
    }

    if(po_right_adc > ADC_THRESHOLD)
    {
        if(po_right_cnt < 3)
        {
            po_right_cnt++;
        }
    }
    else
    {
        po_right_cnt = 0;
    }

    if(po_right_cnt > 3)
    {
        xEventGroupSetBits(xEventGroup, EVENRGROUP_PR);
    }
    else
    {
        xEventGroupClearBits(xEventGroup, EVENRGROUP_PR);
    }
}
